/*
 * tcp_ip_server.h
 *
 *  Created on: Aug 20, 2012
 *      Author: drew
 */

#ifndef TCP_IP_SERVER_H_
#define TCP_IP_SERVER_H_

#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <pthread.h>

typedef void (*vpTcpIpServerRxCallback)(char * cRxBuff, int iLength);

typedef void (*vpTcpIpServerConnectedCallback)( void );

typedef struct
{
  struct sockaddr_in             xServerAddress;
  struct sockaddr_in             xClientAddress;
  struct hostent   *             xpHost;
  char                           cpHostAddress[32];
  int                            iSocket;
  int                            iConnection;
  int                            iIsConnected;
  int                            iPort;
  pthread_t                      xThreadHandle;
  pthread_mutex_t                xMutex;
  vpTcpIpServerRxCallback        vpRxCallback;
  vpTcpIpServerConnectedCallback vpConnectedCallbacke;
} xTcpIpServerDescription, * xpTcpIpServerDescription, * TcpIpServerObject;

/**
 *
 * @param iPort
 * @param vpRxCallback
 * @param vpConnectedCallback
 * @return
 */
xpTcpIpServerDescription xpTcpIpServerCreateObject( int                            iPort,
                                                    vpTcpIpServerRxCallback        vpRxCallback,
                                                    vpTcpIpServerConnectedCallback vpConnectedCallback );

/**
 *
 * @param xpTcpIpServerObj
 */
void vTcpIpServerDestroyObject( xpTcpIpServerDescription xpTcpIpServerObj );

int iTcpIpServerStart( xpTcpIpServerDescription xpTcpIpServerObj );

void vTcpIpServerStop( xpTcpIpServerDescription xpTcpIpServerObj );

int iTcpIpServerWrite( xpTcpIpServerDescription xpTcpIpServerObj,
                       char                   * cpBuff,
                       int                      iLength );

void iTcpIpServerWaitForServerClose( xpTcpIpServerDescription xpTcpIpServerObj );

int iTcpIpServerIsConnected( xpTcpIpServerDescription xpTcpIpServerObj );



#endif /* TCP_IP_SERVER_H_ */
